Component({
  properties: {
    show: {
      type: Boolean,
      value: false //这里为什么是false
    },
    product: {
      type: Object,
      value: null
    },
    specs: {
      type: Array,
      value: []
    },
    tasties: {
      type: Array,
      value: []
    }
  },

  data: {
    selectedSpec: null, //规格
    selectedTaste: null, //口味
    quantity: 1,  //数量
    isCheckThermalBag: 0 //是否选中保温袋，
  },

  methods: {
    //关闭详情弹窗
    closeDetail() {
      this.triggerEvent('closeDetails');
    },
    // 选择规格
    selectSpec(e) {
      const { id } = e.currentTarget.dataset;
      const spec = this.data.specs.find(item => item.id === id);
      const newProduct = this.data.product;
      newProduct.price = '￥'+spec.price;
      this.setData({ selectedSpec: spec,product: newProduct });
    },

    // 选择口味
    selectTaste(e) {
      const { id } = e.currentTarget.dataset;
      const taste = this.data.tasties.find(item => item.id === id);
      this.setData({ selectedTaste: taste });
    },

    //选择保温袋
    selectThermalBag(e) {
       const isCheckThermalBag = this.data.isCheckThermalBag;
       if(isCheckThermalBag === 0){
        this.setData({ isCheckThermalBag: 1 });
       }else{
        this.setData({ isCheckThermalBag: 0 });
       }
    },

    // 添加购物车时增加数量
    increase() {
      const { quantity } = this.data;
      if (quantity < 1000) {
        this.setData({ quantity: quantity + 1 });
      }
    },

    // 添加购物车时减少数量
    decrease() {
      const { quantity } = this.data;
      if (quantity > 1) {
        this.setData({ quantity: quantity - 1 });
      }
    },

    // 加入购物车
    addToCart() {
      const { product, selectedSpec,selectedTaste, quantity,isCheckThermalBag } = this.data;
      if (!selectedSpec) {
        wx.showToast({
          title: '请选择规格',
          icon: 'none'
        });
        return;
      }
      if (!selectedTaste) {
        wx.showToast({
          title: '请选择口味',
          icon: 'none'
        });
        return;
      }
      let tasteIdList = [];
      tasteIdList.push(selectedTaste.id);
      const cartItem = {
        productDetailId: selectedSpec.id,
        tasteIdList: tasteIdList,
        price: product.price + selectedSpec.price,
        quantity: quantity,
        thermalBag: isCheckThermalBag,
        quantity: quantity,
        userId: '1837425962481504256'  //TODO 先写死，后续从token中拿
      };

      this.triggerEvent('addToCart', { cartItem }); //子组件通知父组件
      this.setData({
        selectedSpec: null, //规格
        selectedTaste: null, //口味
        quantity: 1,  //数量
        isCheckThermalBag: 0 //是否选中保温袋，
      });
      this.closeDetail();
    } 
  }
}) 